<?php
class TaxonomiesController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$strTaxonomyCode =  Yii::app()->request->getParam('taxonomy', null);
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//load taxonomy
		$arrTaxonomies = Taxonomies::getInstance()->getList();
		foreach($arrTaxonomies as $R){
			if($strTaxonomyCode == $R['taxonomy_code'])	$intTaxonomyId = $R['taxonomy_id'];
		}
		$intTotalRows = isset($arrTaxonomies[0]['total_rows'])?$arrTaxonomies[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('index', array(
			'arrTaxonomies' => $arrTaxonomies,
			'objPages' => $objPages,		
		));
	}
	
	public function actionForm()
	{
		$intId = Yii::app()->request->getParam('id', null);
		//insert or update taxonomy
		if(Yii::app()->request->isPostRequest)
		{
			$arrParams = Yii::app()->request->getParam('taxonomy', null);
			//insert
			if(empty($intId)){
				$intResult = Taxonomies::getInstance()->insert($arrParams);
				$intPostId = $intResult;
			}
			//update
			else{
				$intResult = Taxonomies::getInstance()->update($intId, $arrParams);
			}

			//done
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			header('Location: '.LINK_ADMINCP_TAXONOMIES);
			exit;
		}
		
		//load form
		$arrItemInfo = array();
		if(!empty($intId)){
			$arrItemInfo = Taxonomies::getInstance()->get($intId);
		}
		//render layout
		$this->render('form', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Taxonomies::getInstance()->delete($intId);
		header('Location:'.LINK_ADMINCP_TAXONOMIES);
		exit;
	}

	public function actionFields(){
		$intTaxonomyId = Yii::app()->request->getParam('taxonomy', null);
		//check if taxonomy is valid
		$taxonomy = Taxonomies::getInstance()->get($intTaxonomyId);
		if(empty($taxonomy)){
			header('Location:'.LINK_ADMINCP_NOTFOUND); exit;	
		}
		//load fields
		$arrFields = Taxonomies::getInstance()->getListFields($intTaxonomyId);
		//render layout
		$this->render('fields', array(
			'arrFields' => $arrFields,
			'intTaxonomyId' => $intTaxonomyId
		));
		$this->setUrl();
	}
	
	public function actionFieldForm(){
		$intTaxonomyId = Yii::app()->request->getParam('taxonomy', null);
		$intId = Yii::app()->request->getParam('id', null);	
		//check if taxonomy is valid
		$taxonomy = Taxonomies::getInstance()->get($intTaxonomyId);
		if(empty($taxonomy)){
			header('Location:'.LINK_ADMINCP_NOTFOUND); exit;	
		}
		//insert or update taxonomy
		if(Yii::app()->request->isPostRequest)
		{
			//get params
			$arrParams['taxonomy_id'] = $intTaxonomyId;
			$arrParams['field_name'] = Yii::app()->request->getParam('field_name', null);
			$arrParams['field_code'] = Yii::app()->request->getParam('field_code', null);
			$arrParams['field_description'] = Yii::app()->request->getParam('field_description', null);
			$arrParams['field_attributes'] = Yii::app()->request->getParam('attributes', null);

			//insert
			if(empty($intId)){
				Taxonomies::getInstance()->insertField($arrParams);
			}			
			//update
			else{
				Taxonomies::getInstance()->updateField($intId, $arrParams);
			}
			
			//done
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			header('Location: '.$this->getUrl());
			exit;
		}
		
		//load fields
		$arrItemInfo = array();
		if(!empty($intId)){
			$arrItemInfo = Taxonomies::getInstance()->getField($intId);
		}
		//render layout
		$this->render('field-form', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionFieldDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Taxonomies::getInstance()->deleteField($intId);
		header('Location:'.$this->getUrl());
		exit;
	}
}